<!--
* @Description: 电子签 - 上上签 - 表单
* @Author: DHL
* @Date: 2022-03-30 13:50:53
 * @LastEditors: DHL
 * @LastEditTime: 2024-01-18 11:49:22
-->
<template>
  <tw-drawer
    v-model="visible"
    title="上上签配置"
    :loading="loading"
    size="50%"
    @close="handleClose"
  >
    <div class="form-wrapper">
      <el-form ref="dbFormRef" :model="dbForm" :rules="rules" label-width="200px">
        <el-form-item label="企业名称" prop="entName">
          <el-input v-model="dbForm.entName" clearable placeholder="请输入企业名称"></el-input>
        </el-form-item>

        <el-form-item label="企业账号" prop="account">
          <el-input v-model="dbForm.account" clearable placeholder="请输入企业账号"></el-input>
        </el-form-item>

        <el-form-item label="开发者ID" prop="developerId">
          <el-input v-model="dbForm.developerId" clearable placeholder="请输入开发者ID"></el-input>
        </el-form-item>

        <el-form-item label="客户端ID" prop="clientId">
          <el-input v-model="dbForm.clientId" clearable placeholder="请输入客户端ID"></el-input>
        </el-form-item>

        <el-form-item label="应用秘钥" prop="accessKey">
          <el-input
            v-model="dbForm.accessKey"
            clearable
            :rows="5"
            type="textarea"
            placeholder="请输入应用秘钥"
          ></el-input>
        </el-form-item>

        <el-form-item label="公钥" prop="publicKey">
          <el-input
            v-model="dbForm.publicKey"
            clearable
            :rows="5"
            type="textarea"
            placeholder="请输入公钥"
          ></el-input>
        </el-form-item>

        <el-form-item label="私钥" prop="privateKey">
          <el-input
            v-model="dbForm.privateKey"
            clearable
            :rows="5"
            type="textarea"
            placeholder="请输入私钥"
          ></el-input>
        </el-form-item>

        <el-form-item label="通知邮箱" prop="notificationEmail">
          <el-input
            v-model="dbForm.notificationEmail"
            clearable
            placeholder="请输入通知邮箱"
          ></el-input>
        </el-form-item>

        <el-form-item label="通知电话" prop="notificationPhone">
          <el-input
            v-model="dbForm.notificationPhone"
            clearable
            placeholder="请输入通知电话"
          ></el-input>
        </el-form-item>

        <el-form-item label="上上签服务器地址" prop="serverHost">
          <el-input
            v-model="dbForm.serverHost"
            clearable
            placeholder="请输入上上签服务器地址"
          ></el-input>
        </el-form-item>

        <el-form-item label="上上签请求加密参数" prop="urlSignParams">
          <el-input
            v-model="dbForm.urlSignParams"
            clearable
            placeholder="请输入上上签请求加密参数"
          ></el-input>
        </el-form-item>

        <el-form-item label="生成的doc文档路径" prop="contractDocPath">
          <el-input
            v-model="dbForm.contractDocPath"
            clearable
            placeholder="请输入生成的doc文档路径"
          ></el-input>
        </el-form-item>

        <el-form-item label="生成的预览pdf文档路径" prop="contractPreviewPdfPath">
          <el-input
            v-model="dbForm.contractPreviewPdfPath"
            clearable
            placeholder="请输入生成的预览pdf文档路径"
          ></el-input>
        </el-form-item>

        <el-form-item label="签约完成之后的pdf文档路径" prop="contractSignPdfPath">
          <el-input
            v-model="dbForm.contractSignPdfPath"
            clearable
            placeholder="请输入签约完成之后的pdf文档路径"
          ></el-input>
        </el-form-item>

        <el-form-item label="签约成功之后的回调地址" prop="signSuccessReturnUrl">
          <el-input
            v-model="dbForm.signSuccessReturnUrl"
            clearable
            placeholder="请输入签约成功之后的回调地址"
          ></el-input>
        </el-form-item>

        <el-form-item label="退租签约成功之后的回调地址" prop="signSuccessReturnUrlForFinish">
          <el-input
            v-model="dbForm.signSuccessReturnUrlForFinish"
            clearable
            placeholder="请输入退租签约成功之后的回调地址"
          ></el-input>
        </el-form-item>

        <el-form-item label="实名认证成功之后的回调地址" prop="realNameAuthSuccessReturnUrl">
          <el-input
            v-model="dbForm.realNameAuthSuccessReturnUrl"
            clearable
            placeholder="请输入实名认证成功之后的回调地址"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <el-button @click="handleClose()">取 消</el-button>
      <el-button type="primary" @click="handleSubmit()"> 提 交 </el-button>
    </template>
  </tw-drawer>
</template>

<script lang="ts" setup>
  import { ElMessage } from 'element-plus'
  import { assign, cloneDeep } from 'lodash-es'
  import { TenantConfigModel } from '@/api/system/model/tenantConfigModel'
  import {
    apiTenantConfigGetByType,
    apiTenantConfigSave,
    apiTenantConfigUpdateById
  } from '@/api/system/tenantConfig'
  import { isEmpty, isNotEmpty } from '@/utils/isUtils'
  import { log } from '../../../utils/logUtils'

  const loading = ref(true)

  /**
   * 显示弹框
   */
  const visible = ref(false)

  const dbFormRef = ref()

  //////////////////////////////////////////////////
  // 表单
  //////////////////////////////////////////////////

  const defaultForm = {
    entName: '',
    account: '',
    developerId: '',
    clientId: '',
    accessKey: '',
    publicKey: '',
    privateKey: '',
    notificationEmail: '',
    notificationPhone: '',
    serverHost: '',
    urlSignParams: '',
    contractDocPath: '',
    contractPreviewPdfPath: '',
    contractSignPdfPath: '',
    signSuccessReturnUrl: '',
    signSuccessReturnUrlForFinish: '',
    realNameAuthSuccessReturnUrl: ''
  }

  let dbForm = reactive(cloneDeep(defaultForm))

  /**
   * 查询的配置信息
   */
  let configForm: TenantConfigModel = {}

  //////////////////////////////////////////////////
  // 表单验证规则
  //////////////////////////////////////////////////

  /**
   * 表单验证规则
   */
  const rules = reactive({
    entName: [
      {
        required: true,
        message: '请输入企业名称',
        trigger: 'blur'
      }
    ],
    account: [
      {
        required: true,
        message: '请输入企业账号',
        trigger: 'blur'
      }
    ],
    developerId: [
      {
        required: true,
        message: '请输入开发者ID',
        trigger: 'blur'
      }
    ],
    clientId: [
      {
        required: true,
        message: '请输入客户端ID',
        trigger: 'blur'
      }
    ],
    accessKey: [
      {
        required: true,
        message: '请输入应用秘钥',
        trigger: 'blur'
      }
    ],
    publicKey: [
      {
        required: true,
        message: '请输入公钥',
        trigger: 'blur'
      }
    ],
    privateKey: [
      {
        required: true,
        message: '请输入私钥',
        trigger: 'blur'
      }
    ],
    notificationEmail: [
      {
        required: true,
        message: '请输入通知邮箱',
        trigger: 'blur'
      }
    ],
    notificationPhone: [
      {
        required: true,
        message: '请输入通知电话',
        trigger: 'blur'
      }
    ],
    serverHost: [
      {
        required: true,
        message: '请输入上上签服务器地址',
        trigger: 'blur'
      }
    ],
    urlSignParams: [
      {
        required: true,
        message: '请输入上上签请求加密参数',
        trigger: 'blur'
      }
    ],
    contractDocPath: [
      {
        required: true,
        message: '请输入生成的doc文档路径',
        trigger: 'blur'
      }
    ],
    contractPreviewPdfPath: [
      {
        required: true,
        message: '请输入生成的预览pdf文档路径',
        trigger: 'blur'
      }
    ],
    contractSignPdfPath: [
      {
        required: true,
        message: '请输入签约完成之后的pdf文档路径',
        trigger: 'blur'
      }
    ],
    signSuccessReturnUrl: [
      {
        required: true,
        message: '请输入签约成功之后的回调地址',
        trigger: 'blur'
      }
    ],
    signSuccessReturnUrlForFinish: [
      {
        required: true,
        message: '请输入退租签约成功之后的回调地址',
        trigger: 'blur'
      }
    ],
    realNameAuthSuccessReturnUrl: [
      {
        required: true,
        message: '请输入实名认证成功之后的回调地址',
        trigger: 'blur'
      }
    ]
  })

  //////////////////////////////////////////////////
  // 函数
  //////////////////////////////////////////////////

  /**
   * 打开弹框
   */
  function handleOpen() {
    visible.value = true
    handleSearch()
  }

  /**
   * 关闭弹框
   */
  function handleClose() {
    dbForm = reactive(cloneDeep(defaultForm))
    visible.value = false
  }

  /**
   * 查询配置信息
   */
  function handleSearch() {
    loading.value = true
    apiTenantConfigGetByType('VISA_SSQ')
      .then((result) => {
        if (result?.success) {
          configForm = result.data
          if (isNotEmpty(configForm.configJson)) {
            assign(dbForm, { remark: configForm.remark }, JSON.parse(configForm.configJson || ''))
          }
        }
      })
      .finally(() => {
        loading.value = false
      })
  }

  /**
   * 提交表单
   */
  function handleSubmit() {
    const config: any = cloneDeep(dbForm)

    const submitParams = {
      id: configForm.id,
      unionId: dbForm.developerId,
      configType: 'VISA_SSQ',
      name: '电子签上上签',
      configJson: JSON.stringify(config)
    }

    dbFormRef.value.validate((valid: boolean) => {
      if (valid) {
        if (isEmpty(configForm.id)) {
          addFun(submitParams)
        } else {
          editFun(submitParams)
        }
      } else {
        log.warn('上上签配置', '表单校验失败')
      }
    })
  }

  /**
   * 新增
   * @param isContinue 是否新增并继续
   */
  function addFun(submitParams) {
    loading.value = true
    apiTenantConfigSave(submitParams)
      .then((result) => {
        if (result?.success) {
          ElMessage.success(result.message)
          handleClose()
        } else {
          ElMessage.error(result?.message)
        }
      })
      .catch(() => {})
      .finally(() => {
        loading.value = false
      })
  }

  /**
   * 编辑
   * @returns
   */
  function editFun(submitParams) {
    loading.value = true
    apiTenantConfigUpdateById(submitParams)
      .then((result) => {
        if (result?.success) {
          ElMessage.success(result.message)
          handleClose()
        } else {
          ElMessage.error(result?.message)
        }
      })
      .catch(() => {})
      .finally(() => {
        loading.value = false
      })
  }

  //////////////////////////////////////////////////
  // 暴露出去，父组件才可以调用到
  //////////////////////////////////////////////////
  defineExpose({
    handleOpen
  })
</script>

<style scoped lang="scss">
  .form-wrapper {
    padding: 10px;
  }
</style>
